package com.learn.ThreadPoolLearnTwo;

import org.apache.log4j.Logger;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/26
 */
public class NewSchuledThreadPoolTest {
    private static final Logger log = Logger.getLogger(NewSchuledThreadPoolTest.class);

    //    如何让每周四18:00:00定时执行任务？
    public static void main(String[] args) {

//        获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

//        获取本周周四时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
//        如果当前时间>本周周四，必须找到下周周四
        if(now.compareTo(time)>0){
            time=time.plusWeeks(1);
        }

        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);

//        initailDelay代表当前时间和周四的时间差
        long initailDelay = Duration.between(now, time).toMillis();
//        period 一周的间隔时间
        long period=1000*60*60*24*7;
        pool.scheduleAtFixedRate(()->{
            System.out.println("running....");
        },initailDelay,period,TimeUnit.MILLISECONDS);

    }


}

